﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using egypt.Logic;
using System.Collections.Generic;
using egypt.Logic.Data;

namespace egypt.Web
{
    public partial class xwzx : System.Web.UI.Page
    {
        DataAdapter da = null;
        int pageIndex = 1;
        int page = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Page.Request.QueryString["Page"] != null)
                {
                    int tmp_pageIndex = 0;
                    if (int.TryParse(Page.Request.QueryString["Page"], out tmp_pageIndex))
                        this.pageIndex = tmp_pageIndex;
                }
                else
                {
                    this.pageIndex = 1;
                }

                Bind();
            }
        }

        private void Bind()
        {

            da = new DataAdapter(System.Configuration.ConfigurationManager.ConnectionStrings["egypt"].ConnectionString);
            int pagesize = 0;
            if(!int.TryParse(System.Configuration.ConfigurationManager.AppSettings["PageSize"], out pagesize))
                pagesize = 10;

            List<News> tmpData = da.GetNewsList(4, pagesize, pageIndex, out page);
            this.GridView1.DataSource = tmpData;
            this.GridView1.DataBind();
            this.hy_first.NavigateUrl = string.Format("?page={0}", 1);
            this.hy_pre.NavigateUrl = string.Format("?page={0}", pageIndex > 1 ? (pageIndex - 1).ToString() : "1");
            this.hy_next.NavigateUrl = string.Format("?page={0}",pageIndex < page ? (pageIndex + 1).ToString() : page.ToString());
            this.hy_last.NavigateUrl = string.Format("?page={0}", page);
            
            if (this.pageIndex > page)
                this.pageIndex = page;

            for (int i = 1; i <= page; i++)
            {
                HyperLink hl = new HyperLink();
                hl.NavigateUrl = string.Format("?page={0}",i);
                hl.Text = i.ToString();
                hl.CssClass = "pager";
                if (i == pageIndex)
                {
                    hl.Style.Add(HtmlTextWriterStyle.FontWeight, "bold");
                }
                this.pager_link.Controls.Add(hl);
            }
        }
    }
    }
